package com.search_wheel.mapper;

import com.search_wheel.pojo.entity.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserMapper {

    /**
     * 根据关键词搜索用户
     * @param keyword 搜索关键词
     * @param offset 偏移量
     * @param limit 限制条数
     * @return 用户列表
     */
    @Select("<script>" +
            "SELECT * FROM t_user WHERE status = 1 " +
            "AND (username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR bio LIKE CONCAT('%', #{keyword}, '%')) " +
            "<choose>" +
            "   <when test='sort == \"time\"'>" +
            "       ORDER BY create_time DESC" +
            "   </when>" +
            "   <otherwise>" +
            "       ORDER BY (username LIKE CONCAT('%', #{keyword}, '%')) DESC, " +
            "                (bio LIKE CONCAT('%', #{keyword}, '%')) DESC" +
            "   </otherwise>" +
            "</choose>" +
            " LIMIT #{offset}, #{limit}" +
            "</script>")
    List<User> searchByKeyword(@Param("keyword") String keyword,
                               @Param("offset") int offset,
                               @Param("limit") int limit,
                               @Param("sort") String sort); // 新增sort参数

    /**
     * 统计搜索用户结果数量
     */
    @Select("SELECT COUNT(*) FROM t_user WHERE status = 1 " +
            "AND (username LIKE CONCAT('%', #{keyword}, '%') " +
            "OR bio LIKE CONCAT('%', #{keyword}, '%'))")
    int countByKeyword(@Param("keyword") String keyword);

    /**
     * 根据ID查询用户
     */
    @Select("SELECT * FROM t_user WHERE id = #{id}")
    User findById(@Param("id") Integer id);

    /**
     * 根据用户名查询用户
     */
    @Select("SELECT * FROM t_user WHERE username = #{username}")
    User findByUsername(@Param("username") String username);

    /**
     * 根据邮箱查询用户
     */
    @Select("SELECT * FROM t_user WHERE email = #{email}")
    User findByEmail(@Param("email") String email);

    /**
     * 插入新用户
     */
    @Insert("INSERT INTO t_user(username, email, bio, status, follower_count, " +
            "following_count, article_count, create_time, last_login_time) " +
            "VALUES(#{username}, #{email}, #{bio}, #{status}, #{followerCount}, " +
            "#{followingCount}, #{articleCount}, #{createTime}, #{lastLoginTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    /**
     * 更新用户信息
     */
    @Update("UPDATE t_user SET username = #{username}, email = #{email}, " +
            "bio = #{bio}, status = #{status} WHERE id = #{id}")
    int update(User user);

    /**
     * 更新最后登录时间
     */
    @Update("UPDATE t_user SET last_login_time = #{lastLoginTime} WHERE id = #{id}")
    int updateLastLoginTime(@Param("id") Integer id,
                            @Param("lastLoginTime") java.time.LocalDateTime lastLoginTime);

    /**
     * 增加粉丝数
     */
    @Update("UPDATE t_user SET follower_count = follower_count + 1 WHERE id = #{id}")
    int incrementFollowerCount(@Param("id") Integer id);

    /**
     * 减少粉丝数
     */
    @Update("UPDATE t_user SET follower_count = follower_count - 1 WHERE id = #{id}")
    int decrementFollowerCount(@Param("id") Integer id);

    /**
     * 增加文章数
     */
    @Update("UPDATE t_user SET article_count = article_count + 1 WHERE id = #{id}")
    int incrementArticleCount(@Param("id") Integer id);

    /**
     * 查询热门用户（按粉丝数排序）
     */
    @Select("SELECT * FROM t_user WHERE status = 1 " +
            "ORDER BY follower_count DESC LIMIT #{limit}")
    List<User> findPopularUsers(@Param("limit") int limit);

    /**
     * 根据状态查询用户列表
     */
    @Select("SELECT * FROM t_user WHERE status = #{status} " +
            "ORDER BY create_time DESC LIMIT #{offset}, #{limit}")
    List<User> findByStatus(@Param("status") Integer status,
                            @Param("offset") int offset,
                            @Param("limit") int limit);

    // UserMapper.java
    @Select("SELECT username FROM t_user " +
            "WHERE username LIKE CONCAT('%', #{keyword}, '%') " +
            "LIMIT #{limit}")
    List<String> findUsernamesContaining(@Param("keyword") String keyword,
                                         @Param("limit") int limit);

    @Select("SELECT username FROM t_user WHERE status = 1")
    List<String> findAllUsernames();
}